package cn.com.zpark.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: cn.com.zpark.filter
 * @ClassName: FilterDemo03
 * @Description:
 * @author: RZS
 * @date: 2021/11/17  17:47
 */
@WebFilter("/user/login/*")
public class FilterDemo03 implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 强转成HttpServletRequest、HttpServletResponse
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 获取session绑定数据
        String name = (String)request.getSession().getAttribute("name");

        if(name == null){
            // 没有登录，拦截，输出提示
            response.getWriter().write("Error");
        }else{
            // 登录过了
            filterChain.doFilter(request, response);
        }
    }
}
